//
//  KSDLoginLogicTool.h
//  KSuodun
//
//  Created by DwZeng on 2022/7/18.
//

#import "KSDLoginLogicTool.h"
@implementation KSDLoginLogicTool

/**
 获取当前跟控制器
 
 @return 跟控制器
 */
+ (KSDTabBarViewController *)getRootViewController{
    
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    KSDTabBarViewController *tabbarController = (KSDTabBarViewController *)window.rootViewController;
    return tabbarController;
}

/**
 获取当前导航控制器
 
 @return 导航控制器
 */
+ (UINavigationController *)getNavigationController{
    
    return [self getCurrentActivityViewController].navigationController;
}


/**
 获取当前窗口正在显示的控制器

 @return UIViewController
 */
+ (UIViewController *)getCurrentActivityViewController {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    LOG(@"window level: %.0f", window.windowLevel);
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    //从根控制器开始查找
    UIViewController *rootVC = window.rootViewController;
    UIViewController *activityVC = nil;
    
    while (true) {
        if ([rootVC isKindOfClass:[UINavigationController class]]) {
            
            activityVC = [(UINavigationController *)rootVC visibleViewController];
            
        } else if ([rootVC isKindOfClass:[UITabBarController class]]) {
            
            activityVC = [(UITabBarController *)rootVC selectedViewController];
            
        } else if (rootVC.presentedViewController) {
            
            activityVC = rootVC.presentedViewController;
            
        } else {
            break;
        }
        
        rootVC = activityVC;
    }
    
    return activityVC;
}




@end
